package com.turing.post.datatranferpmbs.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.datatranferpmbs.util.OldMerchantDao;

/**
 * 卡控制接口实现
 * 
 * @author WZD
 * 
 */
@Stateless(mappedName = "datatranferpmbs.impl.OldMerchantDaoImpl")
@Remote
@Local
public class OldMerchantDaoImpl implements OldMerchantDao {
	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getOldMerchantList() {
		String sql = "select MERCHANTNO,MERCHANTNAME,LINKMAN,LINKTELE,ACTIVATE,to_char(TRANDATE,'yyyy-mm-dd hh:mi:ss'),TRANSTATUS,TRANSEQNO,BALANCE,LASTDAYBALANCE,LASTMONTHBALANCE,REMARK,TRANKEY,SETTLEMENT,DISCOUNTRATE,MAJORCLIENTSRATE,CHECKACCMODE,INTERNALRATE from MERCHANT order by MERCHANTNO asc ";
		Query query = _em.createNativeQuery(sql);
		List<Object[]> obj_list = query.getResultList();
		return obj_list;
	}

	@Override
	@SuppressWarnings("unchecked")
	public HashMap<String, BigInteger> getMerchantSett() {
		String sql = "select t.merchantno,t.settlementrate from mchsettlementrate t order by t.merchantno";
		Query query = _em.createNativeQuery(sql);
		List<Object[]> obj_list = query.getResultList();
		HashMap<String, BigInteger> map = new HashMap<String, BigInteger>();
		BigInteger b = BigInteger.ZERO;
		for (Object[] objs : obj_list) {
			b = BigDecimal.valueOf(10000).subtract(
					new BigDecimal(objs[1].toString()).multiply(BigDecimal
							.valueOf(10000))).toBigInteger();
			if (b.compareTo(BigInteger.valueOf(10000)) >= 0) {
				b = BigInteger.ZERO;
			}
			map.put(objs[0].toString(), b);
		}
		return map;
	}

}
